package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.CgSuppilerInfo;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgSuppilerInfoMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.cg.service.CgSuppilerInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-06
 */
@Service
public class CgSuppilerInfoServiceImpl extends ServiceImpl<CgSuppilerInfoMapper, CgSuppilerInfo> implements CgSuppilerInfoService {
     @Autowired
    CgSupplierMapper cgSupplierMapper;
     @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public boolean insertCgSuppilerInfo(CgSuppilerInfo cgSuppilerInfo) {
        //根据sid查询时哪个一个供应商
        CgSupplier cgSupplier = cgSupplierMapper.selectById(cgSuppilerInfo.getSid());
        //欠款
        if (cgSuppilerInfo.getType()==1){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getOweMoney(),null)){
                cgSupplier.setOweMoney(0f);
            }
            Float oweMoney =cgSupplier.getOweMoney() ;
            cgSuppilerInfo.setLastNums(oweMoney);
            Float nums=  cgSuppilerInfo.getNums();
            Float nowNums= oweMoney+nums;
            cgSuppilerInfo.setNowNums(nowNums);
            cgSupplier.setOweMoney(cgSuppilerInfo.getNowNums());
        }
        //欠票
         if (cgSuppilerInfo.getType()==2){
             if (!AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getOweTicket(),null)){
                 cgSupplier.setOweTicket(0f);
             }
             Float oweTicket = cgSupplier.getOweTicket();
             cgSuppilerInfo.setLastNums(oweTicket);
            Float nums= cgSuppilerInfo.getNums();
             Float nowNums= oweTicket+nums;
             cgSuppilerInfo.setNowNums(nowNums);
             cgSupplier.setOweTicket(cgSuppilerInfo.getNowNums());
         }
        cgSupplierMapper.updateById(cgSupplier);
        boolean insert = super.insert(cgSuppilerInfo);
        return insert;
    }

    @Override
    public List<CgSuppilerInfo> selectBySid(Integer sid, Integer type) {
        Wrapper<CgSuppilerInfo> wrapper=new EntityWrapper<>();
        wrapper.eq("sid",sid);
        wrapper.eq("type",type);
        List<CgSuppilerInfo> cgSuppilerInfos = super.selectList(wrapper);
        for (CgSuppilerInfo cgSuppilerInfo : cgSuppilerInfos) {
            JyUser jyUser = jyUserMapper.selectById(cgSuppilerInfo.getCreator());
            cgSuppilerInfo.setCreatorName(jyUser.getName());
        }
        return cgSuppilerInfos;
    }
}
